package com.bhs.sansong.ui.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.bhs.sansong.R;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.bean.CityBean;
import com.bhs.sansong.utils.MyUtils;
import com.bhs.sansong.utils.ToastUtil;

/**
 * 城市选择弹窗
 */
public class CityPickerDialog extends Dialog implements View.OnClickListener {

    private EditText mEtSearch;
    private TextView mTvCounty;
    private RecyclerView mRvProvince;
    private RecyclerView mRvCity;
    private RecyclerView mRvCounty;
    private BaseActivity baseActivity;
    public CityPickerReuse cityPickerReuse;
    private MyClickListener2 myClickListener;
    private boolean isMultipleChoice;//是否多选
    private TextView mTvTitle;
    private boolean isShowDistrict = true;

    public CityPickerDialog(@NonNull BaseActivity context, boolean isMultipleChoice) {
        super(context, R.style.MyBottomDialogStyle);
        this.baseActivity = context;
        this.isMultipleChoice = isMultipleChoice;
    }

    public CityPickerDialog(@NonNull BaseActivity context) {
        super(context, R.style.MyBottomDialogStyle);
        this.baseActivity = context;
        isShowDistrict = false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_city_picker);
        //获取当前Activity所在的窗体
        Window dialogWindow = getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = MyUtils.getScreenHeight(getContext()) * 7 / 10; //设置Dialog距离底部的距离
        dialogWindow.setAttributes(lp); //将属性设置给窗体
        initView();
        cityPickerReuse = new CityPickerReuse(baseActivity, mRvProvince, mRvCity, mRvCounty, mEtSearch, isMultipleChoice);
        if (isMultipleChoice) {
            mTvTitle.setText("选择地址(多选)");
        }

    }


    private void initView() {
        findViewById(R.id.tv_cancel).setOnClickListener(this);
        findViewById(R.id.tv_confirm).setOnClickListener(this);
        mEtSearch = findViewById(R.id.et_search);
        mTvCounty = findViewById(R.id.tv_county);
        mRvProvince = findViewById(R.id.rv_province);
        mRvCity = findViewById(R.id.rv_city);
        mRvCounty = findViewById(R.id.rv_county);
        mTvTitle = findViewById(R.id.tv_title);
        if(!isShowDistrict){
            mRvCounty.setVisibility(View.GONE);
            mTvCounty.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View view) {

        mEtSearch.clearFocus();
        if (view.getId() == R.id.tv_confirm) {
            if (myClickListener != null) {
                boolean isHas = false;
                if (isMultipleChoice) {
                    for (CityBean cityBean : cityPickerReuse.countyList) {
                        if (cityBean.isCheck()) {
                            isHas = true;
                        }
                    }
                    if (!isHas) {
                        ToastUtil.show("请选择区县");
                        return;
                    }
                }
                dismiss();
                myClickListener.onClick(null, cityPickerReuse.getData());
            }
        } else {
            dismiss();
        }
    }

    public void setMyClickListener(@Nullable MyClickListener2 l) {
        this.myClickListener = l;
    }


}
